<template>
  <el-dialog :title="title" :visible.sync="isShow" width="40%" :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false" @open="openEvent">
    <el-form :model="form">
      <el-form-item label="权限名称" :label-width="formLabelWidth">
        <el-input v-model="form.permissionName" autocomplete="off" style="width: 330px;"></el-input>
      </el-form-item>
      <el-form-item label="排序" :label-width="formLabelWidth">
        <el-input-number style="width: 330px;" v-model="form.permissionSort" :min="1" :max="1000" label="描述文字"></el-input-number>
      </el-form-item>
      <el-form-item label="状态" :label-width="formLabelWidth">
        <el-select v-model="form.status" placeholder="请选择" style="width: 330px;">
          <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注" :label-width="formLabelWidth">
        <el-input v-model="form.remark" autocomplete="off" style="width: 330px;" type="textarea" :rows="3"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="operateInfo">确 定</el-button>
    </div>
  </el-dialog>
</template>
    
<script>
import { addPermission, updatePermission } from '@/api/permission'

export default {
  name: 'PermissionInfoComp',
  data () {
    return {
      form: {
        permissionName: '',
        permissionSort: 1,
        status: '1',
        remark: ""
      },
      formLabelWidth: '120px',
      title: '新增权限',
      statusOptions: [{
        value: '1',
        label: '正常'
      }, {
        value: '0',
        label: '禁用'
      }],
    }
  },
  props: {
    isShow: {
      type: Boolean,
      required: true
    },
    item: {
      type: Object,
      require: false
    }
  },
  methods: {
    openEvent () {
      if (this.item === undefined) {
        this.form.permissionName = ""
        this.form.permissionSort = 1
        this.form.status = "1"
        this.form.remark = ""
        this.title = '新增权限'
      } else {
        this.title = '编辑权限'
        this.form = { ...this.item }
      }
    },
    closeDialog () {
      this.$emit("changeDialogStatus", false);
    },
    result (data) {
      this.$emit("changeDialogStatus", "success");
      if (data.status === 200) {
        this.$message.success("操作成功");
      } else {
        this.$message.error("操作失败，请重试");
      }
    },
    operateInfo () {
      const param = { ...this.form }
      if (this.item === undefined) {
        addPermission(param).then((res) => {
          this.result(res)
        });
      } else {
        updatePermission(param).then((res) => {
          this.result(res)
        });
      }
    }
  }
}
</script>

<style scoped>
</style>
    